|
|
|
|
|
|
|
Functions can be written to perform calculations, test values against an internal table, and parse strings. The return value from a function may be any valid data type, an array, a user-defined type, or an object. The same rules for naming procedures also apply to functions. Also, the same rules apply to functions as they do to procedures regarding public and private scope. |
|
|
|
|
|
|
|
|
You create a function in the exact same way do a procedure. The primary difference is that instead of Sub, you use the keyword Function. Of course, you may choose either private or public scope. You also need to give the function a data type that it will return. You do this with the As keyword after the name of the function. To specify what the return value will be, you set the name of the function equal to the value you want to return. Heres an example: |
|
|
|
 |
|
|
|
|
Private Function EmployeeDisplay() As String Dim strEmp As String
strEmp = "My Last Name is " & txtLast.Text
strEmp = strEmp & " and my first name is " & txtFirst.Text
EmployeeDisplay = strEmp End Function |
|
|
|
|
|
|
|
|
Notice in the function declaration that the As String keywords follow the name of the function. This tells the calling routine what type of data to expect from this function. After you create a string, you can return it by putting the name of the function on the left side of the equal side in an assignment statement. |
|
|
|
|
|
|
|
|
If you call a function from a procedure or another function, youll typically place the return value in a variable or pass it onto another procedure or function. Consider the following code: |
|
|
|
 |
|
|
|
|
Private Sub cmdDisplay_Click()
Dim strName As String
strName = EmployeeDisplay()
MsgBox strName
MsgBox EmployeeDisplay()
End Sub |
|
|
|
|
|